﻿using System;
using System.Linq;
using PortalCurriculo.Domain;

namespace PortalCurriculo.WebUI.CentralEmail
{
    public partial class NovoEmail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            PreencherDropDownLists();
        }

        private void PreencherDropDownLists()
        {
            PreencherContaEmail();
            PreencherGrupoEmail();
            PreencherTipoEmail();
        }

        private void PreencherTipoEmail()
        {
            ddlTipoEmail.DataTextField = "Nome";
            ddlTipoEmail.DataValueField = "Id";
            ddlTipoEmail.DataSource = new TipoEmail().SelectAll<TipoEmail>();
            ddlTipoEmail.DataBind();
        }

        private void PreencherGrupoEmail()
        {
            ddlGrupoEmail.DataTextField = "Nome";
            ddlGrupoEmail.DataValueField = "Id";
            ddlGrupoEmail.DataSource = new GrupoEmail().SelectAll<GrupoEmail>();
            ddlGrupoEmail.DataBind();
        }

        private void PreencherContaEmail()
        {
            ddlConta.DataTextField = "DisplayNameEmail";
            ddlConta.DataValueField = "Id";
            ddlConta.DataSource = (from contaEmail in new Domain.ContaEmail().SelecionarAtivos()
                                   select new
                                              {
                                                  contaEmail.Id,
                                                  DisplayNameEmail = contaEmail.DisplayName + " (" + contaEmail.Email + ")"
                                              }).ToList();
            ddlConta.DataBind();
        }

        protected void rdoGrupoEmail_CheckedChanged(object sender, EventArgs e)
        {
            pnlGrupoEmail.Visible = rdoGrupoEmail.Checked;
            pnlDestinatario.Visible = !rdoGrupoEmail.Checked;
        }

        protected void rdoDestinatario_CheckedChanged(object sender, EventArgs e)
        {
            pnlDestinatario.Visible = rdoDestinatario.Checked;
            pnlGrupoEmail.Visible = !rdoDestinatario.Checked;
        }

        protected void rdoDataAtual_CheckedChanged(object sender, EventArgs e)
        {
            txtEnviarEm.Visible = false;
            lblDataEnvio.Visible = false;
        }

        protected void rdoAgendar_CheckedChanged(object sender, EventArgs e)
        {
            txtEnviarEm.Visible = true;
            lblDataEnvio.Visible = true;
        }

        protected void lnkEditDisplayName_Click(object sender, EventArgs e)
        {
            if (txtDisplayName.Visible)
            {
                lnkEditDisplayName.Text = "Alterar nome email";   
            }
            else
            {
                txtDisplayName.Text = ddlConta.SelectedItem.Text.Split('(')[0];
                lnkEditDisplayName.Text = "Cancelar";
            }

            txtDisplayName.Visible = !txtDisplayName.Visible;
        }

        protected void chkTipoEmail_CheckedChanged(object sender, EventArgs e)
        {
            pnlEnviarEmail.Visible = !chkTipoEmail.Checked;
            pnlTipoEmail.Visible = chkTipoEmail.Checked;
        }

        protected void lnkSalvar_Click(object sender, EventArgs e)
        {
            //Apenas gravará no banco sem enviar email por esta tela
            var email = new Email
                            {
                                Assunto = txtAssunto.Text,
                                Ativo = true,
                                Conteudo = edtConteudo.Content,
                                DestinatarioDisplayName = txtDestinatarioDisplayName.Text,
                                DestinatarioEmail = txtDestinatarioEmail.Text,
                                DisplayName = txtDisplayName.Text,
                                EnviarEm = string.IsNullOrEmpty(txtEnviarEm.Text) ? (DateTime?) null : DateTime.Parse(txtEnviarEm.Text),
                                IdContaEmail = int.Parse(ddlConta.SelectedValue),
                                IdGrupoEmail = chkTipoEmail.Checked ? (int?)null : int.Parse(ddlGrupoEmail.SelectedValue),
                                IdTipoEmail = chkTipoEmail.Checked ? int.Parse(ddlTipoEmail.SelectedValue) : (int?)null,  
                                DataAlteracao = DateTime.Now,
                                DataInclusao = DateTime.Now
                            };

            email.Save();
        }
    }
}